package com.miaostars.myaifriend;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.sound.sampled.*;
import java.nio.file.Path;

public class AudioPlayer {
    private final Player player;
    private final Main plugin;

    public AudioPlayer(Player player) {
        this.player = player;
        this.plugin = (Main) Bukkit.getPluginManager().getPlugin("MyAIFriend");
    }

    public void play(Path path) {
        Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
            try (AudioInputStream stream = AudioSystem.getAudioInputStream(path.toFile())) {
                Clip clip = AudioSystem.getClip();
                clip.open(stream);
                clip.start();
                
                // 播放完成后删除文件
                clip.addLineListener(e -> {
                    if (e.getType() == LineEvent.Type.STOP) {
                        clip.close();
                        path.toFile().delete();
                    }
                });
            } catch (Exception e) {
                player.sendMessage("§c播放失败: " + e.getMessage());
            }
        });
    }
}